#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
using namespace cv;
using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->processButton->setEnabled(false);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_openButton_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
           tr("Open Image"), ".", tr("Image Files (*.png *.jpg *.bmp)"));
       m_originalImg = cv::imread(fileName.toUtf8().data());
       if(m_originalImg.data) {
           this->displayImage();
           ui->processButton->setEnabled(true);
       }
}


void MainWindow::on_processButton_clicked()
{
    int thresh = ui->cthresh_slider->value();
    RNG rng(12345);
    Mat src_gray;
    // konvertieren und blur
    cvtColor( m_originalImg, src_gray, CV_BGR2GRAY );
    blur( src_gray, src_gray, Size(3,3) );

    Mat threshold_output;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    // ecken finden
    Canny( src_gray, threshold_output, thresh, thresh*2, 3 );
    //threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );

    /*cv::adaptiveThreshold(src_gray,threshold_output,
                          255,
                          cv::ADAPTIVE_THRESH_MEAN_C,
                          cv::THRESH_BINARY,
                          151, // block size
                          1);*/

    namedWindow( "Thresh", CV_WINDOW_AUTOSIZE );
    imshow( "Thresh", threshold_output );

    // konturen finden
    findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    // konturen zu polygonen approximieren + bounding boxen erstellen
    vector<vector<Point> > contours_poly( contours.size() );
    vector<Rect> boundRect( contours.size() );

    for( unsigned int i = 0; i < contours.size(); i++ )
    {
        approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
        boundRect[i] = boundingRect( Mat(contours_poly[i]) );
    }

    // polygon + bounding box zeichnen
    Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
    cv::Scalar colorKontur = cv::Scalar(255,0,0);
    cv::Scalar colorBB = cv::Scalar(0,0,255);
    for( unsigned int i = 0; i< contours.size(); i++ )
     {
       drawContours( drawing, contours_poly, i, colorKontur, 1, 8, vector<Vec4i>(), 0, Point() );
       rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), colorBB,
                  1, // dicke
                  8, // linientyp
                  0 );
     }

    namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
    imshow( "Contours", drawing );
}


void MainWindow::displayImage()
{
    // Aendere Farbkanalanordnung fuer Qt
    cv::Mat image = m_originalImg;
    cv::cvtColor(image, image, CV_BGR2RGB);

    // Erzeuge ein Qt image
    QImage img = QImage((uchar*) image.data, image.cols, image.rows, image.step, QImage::Format_RGB888);//.scaledToWidth(ui->processImgLabel->width());

    if(ui->processImgLabel) {
        // Erzeuge eine Pixmap
        QPixmap pixmap = QPixmap::fromImage(img);

        // Zeige Pixmap im Label an
        ui->processImgLabel->setPixmap(pixmap);
    }
}
